﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using Etong.Web;
/// <summary>
///BeforeFactory 的摘要说明
/// </summary>
public class BeforeFactory
{
    public static IBeforeDoing GetBefore(string className)
    {
        try
        {
            return (IBeforeDoing)Assembly.Load("App_Code").CreateInstance(className);
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    public static IBeforeDoing GetBefore(ConfigTable cTab, BeforeType bfType)
    {
        IBeforeDoing bfDoing = null;
        switch (bfType)
        {
            case BeforeType.BeforeInsert:
                bfDoing = GetBefore(cTab.BeforeInsertClassName);
                break;
            case BeforeType.BeforeUpdate:
                bfDoing = GetBefore(cTab.BeforeUpdateClassName);
                break;
            case BeforeType.BeforeDelete:
                bfDoing = GetBefore(cTab.BeforeDeleteClassName);
                break;
        }
        return bfDoing;
    }

    public static void BeforeDo(ConfigTable cTab, BeforeType bfType, string PKValue)
    {
        IBeforeDoing bfDoing = GetBefore(cTab, bfType);
        if (bfDoing != null)
        {
            switch (bfType)
            {
                case BeforeType.BeforeInsert:
                    bfDoing.BeforeInsert();
                    break;
                case BeforeType.BeforeUpdate:
                    bfDoing.BeforeUpdate(PKValue);
                    break;
                case BeforeType.BeforeDelete:
                    bfDoing.BeforeDelete(PKValue);
                    break;
            }
        }
    }
}
